﻿using System;
using System.Data;
using System.Windows.Forms;
using MediaPortal.GUI.Library;
using MediaPortal.Dialogs;
using MediaPortal.Player;
using System.Data.SQLite;
 

namespace MP_Plugin_Tutorial
{
    public class SimTV_Plugin :GUIWindow, ISetupForm
    {

        string skinPath = "SimTV_Plugin.xml";   //Path To Skin XML
        [SkinControlAttribute(2)] protected GUIButtonControl buttonOne = null;
        [SkinControlAttribute(3)] protected GUIButtonControl buttonTwo = null;
              
        public SimTV_Plugin()
        {
            
        }

        #region ISetupForm Members

        public override bool Init()
        {
            //Add An Action Handler So This Plugin Can Respond To User Actions
            GUIWindowManager.OnNewAction += new OnActionHandler(this.OnAction2);
            return Load(GUIGraphicsContext.Skin + @"\" + skinPath);
        }

        //Returns the name of the plugin which is shown in the plugin menu
        public string PluginName()
        {
            return "MyFirstPlugin";
        }

        //Returns the description of the plugin shown in the plugin menu
        public string Description()
        {
            return "My First Plugin Description...";
        }

        //Returns the autiof of the plgin which is shown in the plugin menu
        public string Author()
        {
            return "SammyK";        
        }

        //Show the setup dialog
        public void ShowPlugin()
        {
            MessageBox.Show("Nothing to configure, this is just an example...");
        }

        //Indicates whether plugin can be enabled / disabled
        public bool CanEnable()
        {
            return true;
        }

        //Get Window-ID
        public int GetWindowId()
        {
            return 123456;
        }

        //indicates if plugin is enabled by default
        public bool DefaultEnabled()
        {
            return true;
        }
        
        //indicates if a plugin has its own setup screen
        public bool HasSetup()
        {
            return true;
        }

        public bool GetHome(out string strButtonText, out string strButtonImage, out string strButtonImageFocus, out string strPictureImage)
        {
            strButtonText = PluginName();
            strButtonImage = String.Empty;
            strButtonImageFocus = String.Empty;
            strPictureImage = String.Empty;
            return true;
        }

        public override int  GetID
        {
	          get 
	        { 
                  return 123456;
	        }
	          set 
	        { 
                
	        }
        }

        protected override void OnClicked(int controlId, GUIControl control, MediaPortal.GUI.Library.Action.ActionType actionType)
        {
            if (control == buttonOne)
            {
                OnButtonOne();
            }
            else if (control == buttonTwo)
            {
                OnButtonTwo();
            }
            base.OnClicked(controlId, control, actionType);
        }

        private void OnButtonOne()
        {
            GUIDialogOK dlg = (GUIDialogOK)GUIWindowManager.GetWindow((int)GUIWindow.Window.WINDOW_DIALOG_OK);
            

            //Connect To DB
            DB_Connect();

            //Execute Query
            DataTable DB_Resutls = DB_executeQuery(DB_MPTV_Connection, "SELECT Pretty_Name FROM Online_Series WHERE ID='72173'");

            string queryStingResult = DB_Resutls.Rows[0].ItemArray[0].ToString();
            
            dlg.SetHeading("Query Exectued. See Results Below...");
            dlg.SetLine(1, queryStingResult);
            dlg.SetLine(2, String.Empty);
            dlg.SetLine(3, String.Empty);
            dlg.DoModal(GUIWindowManager.ActiveWindow);


        }

        public void OnAction2(MediaPortal.GUI.Library.Action action)
        {
            switch (action.wID)
            {
                case MediaPortal.GUI.Library.Action.ActionType.ACTION_STEP_BACK:
                    if (g_Player.CurrentPosition < 100)
                    {
                        g_Player.SeekAbsolute(100);
                    }
                    break;
                case MediaPortal.GUI.Library.Action.ActionType.ACTION_PAUSE:
                    g_Player.SeekAbsolute(100);
                    break;
                default:
                    base.OnAction(action);
                    break;
            }
        }

        private void OnButtonTwo()
        {
            //String result = g_Player.Play("C:\Users\Sammy_K\Desktop\reno911.avi", g_Player.MediaType.Video);
            g_Player.Play("C:/Users/Sammy_K/Desktop/reno911.avi", g_Player.MediaType.Video);
            g_Player.SeekAbsolute(30);

            



            //g_Player.Play("monkey.avi");

            /*
            GUIDialogOK dlg = (GUIDialogOK)GUIWindowManager.GetWindow((int)GUIWindow.Window.WINDOW_DIALOG_OK);
            dlg.SetHeading("Button has been pressed");
            dlg.SetLine(1, "You pressed button 2");
            dlg.SetLine(2, String.Empty);
            dlg.SetLine(3, String.Empty);
            dlg.DoModal(GUIWindowManager.ActiveWindow);
             */
        }

        //SQLite Database Interaction
        private string dbPath = "C:/ProgramData/Team MediaPortal/MediaPortal/database";
        private string dbName = "TVSeriesDatabase4.db3";
        private SQLiteConnection DB_MPTV_Connection;
        private SQLiteCommand DB_Command;
        private SQLiteDataAdapter DB_Adapter;
        private DataSet DS = new DataSet();        

        private void DB_Connect()
        {
            DB_MPTV_Connection = new SQLiteConnection("Data Source=C:/ProgramData/Team MediaPortal/MediaPortal/database/TVSeriesDatabase4.db3;Version=3;New=False;Compress=True");
        }

        private DataTable DB_executeQuery(SQLiteConnection dbConnection, string queryString)
        {
            DB_Connect();
            dbConnection.Open();
            DB_Command = dbConnection.CreateCommand();
            DB_Adapter = new SQLiteDataAdapter(queryString, dbConnection);
            DS.Reset();
            DB_Adapter.Fill(DS);
            DataTable results = DS.Tables[0]; 
            dbConnection.Close();
            return results;
        }

        #endregion
        
    }
}
